package com.woniuxy.ai.util;

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class AiUtil {
    public static String askSomething(String url, String json) {
        // 构建请求数据
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        Request.Builder requestbuilder = new Request.Builder()
                .url(url)
                .post(RequestBody.create(mediaType, json));

        Request request = requestbuilder.build();
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            return "Request Failed";
        }
    }
    public static void bigModel(String message) {
        String url = "http://localhost:11434/api/generate";
        // 为了直观构建JSON字符串，此处使用单引号替换的方案，而非使用Jackson等库
        String json = "{'model':'deepseek-r1:1.5b','prompt': '%s', 'stream': false}"
                .formatted(message)
                .replace("'", "\"");
        String response = askSomething(url, json);
        try {
            Map<String, Object> map = JSON.parseObject(response, Map.class);
            System.out.println("AI 回答：" + map.get("response"));

            // 可选：打印整个 map 查看结构
            // System.out.println(map);
        } catch (Exception e) {
            System.out.println("JSON 解析失败");
            System.out.println(response);
        }
    }
}
